<?php
namespace app\index\controller;

use think\App;
use think\facade\Db;
use think\facade\View;

use app\index\controller\Base;
use app\admin\model\TopicCategory as CategoryModel;
use app\admin\model\TopicContent as ContentModel;
use app\admin\model\TopicSun as SunModel;
use app\admin\model\Article as ArticleModel;
use app\admin\model\TopicInfo as TopicInfoModel;
use app\admin\model\TemplateList as TemplateListModel;

/**
 * 专题
 * @author huwhis@163.com
 */
class Topic extends Base
{
    public function index($tid = 0)
    {
        if (!$tid) {
            throw new \think\exception\HttpException(404, '页面不存在');
        }

        $topicInfo = ContentModel::field('id,cid,title,topic_content,username,clicks,create_time')->where('status', 1)->find($tid);

        return View::display($topicInfo->topic_content);
    }

    public function sun($tid, $id)
    {
        if (!$id) {
            throw new \think\exception\HttpException(404, '页面不存在');
        }

        $topicInfo = ContentModel::field('id,cid,title,topic_url,username,clicks,create_time')->where('status', 1)->find($tid);

        View::assign('topicInfo', $topicInfo);

        $topicSunInfo = SunModel::field('id,title,template_list_id,topic_content,type,clicks,create_time')->find($id);

        if ($topicSunInfo->type==3) {
            return View::display($topicSunInfo->topic_content);
        }

        View::assign('topicSunInfo', $topicSunInfo);

        $list = TopicInfoModel::where('tsid', $topicSunInfo->id)
            ->alias('tinfo')
            ->join('article a', 'tinfo.aid=a.id')
            ->field('a.id, a.title, a.summary, a.create_time, a.clicks, a.title_pic')        
            ->select();

        View::assign('list', $list);

        if ($topicSunInfo->template_list_id == 0) {
            return View::fetch('article/index');
        }

        $templateContent = TemplateListModel::where('id', $topicSunInfo->template_list_id)->find();

        return View::display($templateContent);
    }
}